/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bice.service;

import com.bice.dao.Dao;
import com.bice.dao.IEntity;
import com.bice.entity.User;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

/**
 *
 * @author sirius
 */
@Service
public class UserService {
    
    @Resource
    private Dao<User, Serializable> dao;

    public UserService() {
    }
    
    public void save(User user) {
        if(user.getName() != null) user.setName(user.getName().trim());
        if(user.getNickname() != null) user.setNickname(user.getNickname().trim());
        user.setCode(user.getNickname());
        dao.save(user);
    }
    
    public List<User> getUserByName() {
        DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
        criteria.addOrder(Order.asc("name"));
        return dao.get(criteria);
    }
    
    public User getUser(String nickName) {
        DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
        criteria.add(Restrictions.eq("nickname", nickName));
        return dao.getUnique(criteria);
    }
    
    public List<User> getContacts(String nickName) {
        DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
        criteria.add(Restrictions.not(Restrictions.eq("nickname", nickName)));
        return dao.get(criteria);
    }
    
    public List<User> getUser(DetachedCriteria criteria, int first, int pageSize) {
        return dao.get(criteria, first, pageSize);
    }
    
    public int count(DetachedCriteria criteria) {
        return dao.count(criteria);
    }
    
}   